package com.codedancer.campus.manager.api.commodity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.commodity.ro.CommodityExamineRo;
import com.codedancer.campus.manager.api.commodity.ro.CommodityPageSearchRo;
import com.codedancer.campus.manager.service.ICommodityService;
import com.codedancer.campus.manager.service.bo.commodity.CommodityExamineBo;
import com.codedancer.campus.manager.service.bo.commodity.CommodityPageSearchBo;
import com.codedancer.campus.manager.service.vo.commodity.CommodityPageVo;
import com.codedancer.campus.manager.service.vo.commodity.CommodityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/commodity")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "商品Api", description = "商品Api", tags = "商品Api")
public class CommodityApi extends BaseApi {

    @Autowired
    private ICommodityService commodityService;

    @GetMapping("/page")
    @ApiOperation("分页查询商品")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<IPage<CommodityPageVo>> page(@ApiParam(value = "商品分页搜索Ro", required = false)
        CommodityPageSearchRo commodityPageSearchRo) {
        CommodityPageSearchBo commodityPageSearchBo = BeanUtil.copyProperties(commodityPageSearchRo,
            CommodityPageSearchBo.class);
        return success(commodityService.page(commodityPageSearchBo));
    }

    @PutMapping("/drop/{uuid}")
    @ApiOperation("下架")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> drop(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.drop(uuid);
        return success();
    }

    @PutMapping("/put/{uuid}")
    @ApiOperation("上架")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> put(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.put(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.delete(uuid);
        return success();
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("商品详情")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<CommodityVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        return success(commodityService.detail(uuid));
    }

    @PutMapping("/no-pass/{uuid}")
    @ApiOperation("审核未通过")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> noPass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "商品审核Ro", required = true) CommodityExamineRo commodityExamineRo) {
        CommodityExamineBo commodityExamineBo = BeanUtil
            .copyProperties(commodityExamineRo, CommodityExamineBo.class);
        commodityExamineBo.setCurrentUserUuid(currentLoginUuid());
        commodityExamineBo.setUuid(uuid);
        commodityService.noPass(commodityExamineBo);
        return success();
    }

    @PutMapping("/pass/{uuid}")
    @ApiOperation("审核通过")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> pass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "商品审核Ro", required = true) CommodityExamineRo commodityExamineRo) {
        CommodityExamineBo commodityExamineBo = BeanUtil
            .copyProperties(commodityExamineRo, CommodityExamineBo.class);
        commodityExamineBo.setUuid(uuid);
        commodityExamineBo.setCurrentUserUuid(currentLoginUuid());
        commodityService.pass(commodityExamineBo);
        return success();
    }

    @PostMapping("/export")
    @ApiOperation("导出商品")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public void export(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(value = "商品分页搜索Ro", required = false)
            CommodityPageSearchRo commodityPageSearchRo) {
        CommodityPageSearchBo commodityPageSearchBo = BeanUtil
            .copyProperties(commodityPageSearchRo, CommodityPageSearchBo.class);
        commodityService.export(request, response,commodityPageSearchBo);
    }

    @PutMapping("/batch-drop")
    @ApiOperation("批量下架")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> batchDrop(@RequestBody List<String> uuids) {
        commodityService.batchDrop(uuids);
        return success();
    }

    @PutMapping("/batch-put")
    @ApiOperation("批量上架")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> batchPut(@RequestBody List<String> uuids) {
        commodityService.batchPut(uuids);
        return success();
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation("批量删除")
    @InterfacePermission(encodes = {"superAdmin","commodityAdmin"})
    public ApiResponse<Void> batchDelete(@RequestBody List<String> uuids) {
        commodityService.batchDelete(uuids);
        return success();
    }
}
